/*
 * @(#)IndexSenseWordNet.java	1.0 09/01/07
 *
 * Copyright 2007 Fabio Gasparetti. All rights reserved.
 */

package org.tabularium.text.nlp.wordnet.jwi;

import java.net.URL;
import java.io.*;
import java.util.*;
import java.util.prefs.Preferences;

import org.tabularium.text.nlp.wordnet.*;

import edu.mit.jwi.dict.IDictionary;
import edu.mit.jwi.morph.SimpleStemmer;

/**
 * Provides the functionality to retrieve tag_cnt (number of times the sense is
 * tagged in various semantic concordance texts).
 * 
 * @author Fabio Gasparetti
 * @version 1.0, 09/01/07
 */
public abstract class IndexSenseWordNet implements WordNet {
	protected SynsetFreq[] synsetFreqs = null;

	protected IDictionary dict = null;

	protected String wordNetDictDir = null;

	/**
	 * Initializes retrieving the file system location of WordNet dictionary from
	 * user's preferences.
	 */
	public void init() throws Exception {
		Preferences prefs = Preferences.userRoot().node(
				"org/tabularium/text/nlp");
		String path = prefs.get("wordnet.home",
				"/home/fabio/projects/cues/WordNet-3.0/dict");
		this.init(path);
	}

	/**
	 * Initializes with the file system location of WordNet dictionary.
	 */
	public void init(String wordNetDictDir) throws Exception {
		this.wordNetDictDir = wordNetDictDir;
		URL url = new URL("file", null, wordNetDictDir);
		dict = new edu.mit.jwi.dict.Dictionary(url);
		dict.open();
	}

	public int getSynsetFreq(Synset s) {
		try {
			if (synsetFreqs == null)
				loadSynsetFreqs();
		} catch (Exception ex) {
			ex.printStackTrace();
			return -1;
		}
		SynsetFreq sf = new SynsetFreq(Long.parseLong(s.getId()), 0);
		int idx = Arrays.binarySearch(synsetFreqs, sf);
		return idx > 0 ? synsetFreqs[idx].freq : 0;
	}

	protected void loadSynsetFreqs() throws IOException {
		TreeMap m = new TreeMap();
		File f = new File(wordNetDictDir, "index.sense");
		FileReader reader = new FileReader(f);
		BufferedReader bufReader = new BufferedReader(reader);
		String line;
		while ((line = bufReader.readLine()) != null) {
			String[] splits = line.split(" ");
			String oldFreq = (String) m.get(splits[1]);
			if (oldFreq != null) {
				int newFreq = Integer.parseInt(splits[3])
						+ Integer.parseInt(oldFreq);
				m.put(splits[1], Integer.toString(newFreq));
			} else {
				m.put(splits[1], splits[3]);
			}
		}
		bufReader.close();
		reader.close();
		synsetFreqs = new SynsetFreq[m.size()];
		Iterator keys = m.keySet().iterator();
		int i = 0;
		while (keys.hasNext()) {
			String offset = (String) keys.next();
			int tag_cnt = Integer.parseInt((String) m.get(offset));
			synsetFreqs[i++] = new SynsetFreq(Long.parseLong(offset), tag_cnt);
		}
	}

}

class SynsetFreq implements Comparable {
	long synset_offset;

	int freq;

	public SynsetFreq(long o, int f) {
		synset_offset = o;
		freq = f;
	}

	public int compareTo(Object o) {
		if (this == o)
			return 0;
		SynsetFreq sf = (SynsetFreq) o;
		if (synset_offset < sf.synset_offset)
			return -1;
		if (synset_offset > sf.synset_offset)
			return 1;
		return 0;
	}
}